package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.CheckGroup;
import com.itheima.health.pojo.entity.Setmeal;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.pojo.result.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private SetmealService setmealService;

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        try {
            String originalFilename = imgFile.getOriginalFilename();
            String endName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = endName+UUID.randomUUID();
            String url = aliOssUtil.upload(imgFile.getBytes(), fileName);
            return new Result(true, MessageConst.ACTION_SUCCESS,url);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }
        return new Result(false, MessageConst.ACTION_FAIL);

    }

    /**
     * 新增
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, @RequestParam("checkgroupIds") List<Integer> checkGroupIds){
        log.info("[套餐-新增]data:{},{}",setmeal,checkGroupIds);
        setmealService.add(setmeal,checkGroupIds);
        return new Result(true,MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBeanDTO queryPageBean) {
        log.info("[套餐-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = setmealService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true,MessageConst.ACTION_SUCCESS, pageResult);
    }




}
